package com.wx.web.controller.course;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.util.StringUtil;
import com.wx.business.course.IxbCourseCommentService;
import com.wx.business.util.YesOrNo;
import com.wx.model.IxbCourseComment;
import com.wx.model.IxbCourseCommentExample;
import com.wx.model.IxbUser;
import com.wx.model.base.BaseIxbCourseCommentExample.Criteria;

/**
 * 课程评论控制器
 *
 * @author wemo
 * @Date 2018-08-08 18:27:50
 */
@Controller
@RequestMapping("/comment")
public class CourseCommentController {
	
	 @Autowired
     public  IxbCourseCommentService  courseCommentService;

    
     /**
      * 课程评论
      * @param sid
      * @return
      */
    @RequestMapping(value = "/publish")
    @ResponseBody
    public JsonResult CourseComment(HttpServletRequest request,String courseId ,String courseTitle,int starLevel,String content,String commentImageUrl ) {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");

    	
    	courseCommentService.insertCourseComment(content, courseId, courseTitle, starLevel, user.getSid(), user.getUserName(), user.getUserPicUrl(),commentImageUrl);

		return new JsonResult(true,null);
    
    }
    
  

    /**
     * 课程评论列表 
     * @param request
     * @param courseId
     * @param start  
     * @param pageSize
     * @param condition   0 所有   1 最新   2 有图  
     * @return
     */
   @RequestMapping(value = "/commentList")
   @ResponseBody
   public JsonResult CourseCommentList(HttpServletRequest request,String courseId ,int start,int pageSize,int condition) {
        IxbCourseCommentExample  ext=new IxbCourseCommentExample();
        Criteria cri= ext.createCriteria().andCourseIdEqualTo(courseId);
        if(StringUtil.isNotEmpty(condition+"")&&condition==2)
        {
        	cri.andIsPicEqualTo(YesOrNo.YES);
        }
        String orderByClause=null;
        if(StringUtil.isNotEmpty(condition+"")&&condition==1)
        {
        	orderByClause="  create_time desc";
        }
        Pagination<IxbCourseComment> page= courseCommentService.queryByPage(ext, start, pageSize, orderByClause);
        
		return new JsonResult(true,page);
   
   }
   
   

    
}
